using System;
using System.Drawing;

namespace MinesweeperGame
{
    /// <summary>
    /// 表示扫雷游戏中的一个方块
    /// </summary>
    public class Cell
    {
        /// <summary>
        /// 方块的行索引
        /// </summary>
        public int Row { get; set; }

        /// <summary>
        /// 方块的列索引
        /// </summary>
        public int Col { get; set; }

        /// <summary>
        /// 是否为地雷
        /// </summary>
        public bool IsMine { get; set; }

        /// <summary>
        /// 是否已被揭开
        /// </summary>
        public bool IsRevealed { get; set; }

        /// <summary>
        /// 是否被标记为地雷
        /// </summary>
        public bool IsFlagged { get; set; }

        /// <summary>
        /// 周围地雷数量
        /// </summary>
        public int AdjacentMineCount { get; set; }

        /// <summary>
        /// 方块状态
        /// </summary>
        public CellState State
        {
            get
            {
                if (IsFlagged)
                    return CellState.Flagged;
                if (!IsRevealed)
                    return CellState.Hidden;
                if (IsMine)
                    return CellState.Exploded;
                if (AdjacentMineCount == 0)
                    return CellState.Empty;
                return CellState.Numbered;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="row">行索引</param>
        /// <param name="col">列索引</param>
        public Cell(int row, int col)
        {
            Row = row;
            Col = col;
            IsMine = false;
            IsRevealed = false;
            IsFlagged = false;
            AdjacentMineCount = 0;
        }

        /// <summary>
        /// 重置方块状态
        /// </summary>
        public void Reset()
        {
            IsMine = false;
            IsRevealed = false;
            IsFlagged = false;
            AdjacentMineCount = 0;
        }

        /// <summary>
        /// 切换标记状态
        /// </summary>
        public void ToggleFlag()
        {
            if (!IsRevealed)
            {
                IsFlagged = !IsFlagged;
            }
        }

        /// <summary>
        /// 揭开方块
        /// </summary>
        public void Reveal()
        {
            if (!IsFlagged && !IsRevealed)
            {
                IsRevealed = true;
            }
        }

        /// <summary>
        /// 获取显示文本
        /// </summary>
        /// <returns>显示文本</returns>
        public string GetDisplayText()
        {
            if (IsFlagged)
                return "🚩";
            if (!IsRevealed)
                return "";
            if (IsMine)
                return "💣";
            if (AdjacentMineCount == 0)
                return "";
            return AdjacentMineCount.ToString();
        }

        /// <summary>
        /// 获取方块颜色
        /// </summary>
        /// <returns>方块颜色</returns>
        public Color GetDisplayColor()
        {
            if (IsFlagged)
                return Color.FromArgb(192, 192, 192); // 浅灰色背景，红色旗帜
            if (!IsRevealed)
                return Color.FromArgb(192, 192, 192); // Windows扫雷的未揭开方块颜色
            if (IsMine)
                return Color.FromArgb(192, 192, 192); // 浅灰色背景
            if (AdjacentMineCount == 0)
                return Color.FromArgb(192, 192, 192); // 浅灰色背景

            // Windows扫雷的数字颜色
            switch (AdjacentMineCount)
            {
                case 1: return Color.FromArgb(0, 0, 255);     // 蓝色
                case 2: return Color.FromArgb(0, 128, 0);     // 绿色
                case 3: return Color.FromArgb(255, 0, 0);     // 红色
                case 4: return Color.FromArgb(0, 0, 128);     // 深蓝色
                case 5: return Color.FromArgb(128, 0, 0);     // 深红色
                case 6: return Color.FromArgb(0, 128, 128);   // 青色
                case 7: return Color.FromArgb(0, 0, 0);       // 黑色
                case 8: return Color.FromArgb(128, 128, 128); // 灰色
                default: return Color.FromArgb(0, 0, 0);
            }
        }
    }

    /// <summary>
    /// 方块状态枚举
    /// </summary>
    public enum CellState
    {
        Hidden,     // 隐藏
        Revealed,   // 已揭开
        Flagged,    // 已标记
        Exploded,   // 爆炸
        Empty,      // 空白
        Numbered    // 数字
    }
} 